"""""
 数据分析实验四
 选题：新冠疫情数据可视化分析2.0
 初次编写时间：2022/5/1
 更改时间：2022/5/4
 更改内容：更改数据爬取网址，将存储格式更改为mysql 数据库存储格式
 作者：王庆龙 20194120
 本文件实现数据的爬取
 本文件数据来自百度
"""""
import requests
import json
from lxml import etree
import time


def get_china_data():
    """

    :return: history,                       历史数据
             now,                           最新数据
             province_details,              各省数据
             city_details,                  各市数据
             highDangerousAreas,            高风险区
             middleDangerousAreas           中风险区

    """
    url = "https://voice.baidu.com/act/newpneumonia/newpneumonia"
    response = requests.get(url)
    html = etree.HTML(response.text)
    result = html.xpath('//script[@type="application/json"]/text()')
    result = json.loads(result[0])

    # 历史数据获取
    history = {}
    history_len = len(result["component"][0]["trend"]["updateDate"])
    for i in range(history_len):
        ds = "2021." + result["component"][0]["trend"]["updateDate"][i]
        tup = time.strptime(ds, "%Y.%m.%d")  # 匹配时间
        ds = time.strftime("%Y-%m-%d", tup)  # 改变时间格式

        # 累计确诊
        curConfirm = result["component"][0]["trend"]["list"][0]["data"][i]

        # 累计治愈
        crued = result["component"][0]["trend"]["list"][2]["data"][i]

        # 累计死亡
        died = result["component"][0]["trend"]["list"][3]["data"][i]

        # 新增确诊
        confirmedRelative = result["component"][0]["trend"]["list"][4]["data"][i]

        # 新增无症状
        asymptomaticRelative = result["component"][0]["trend"]["list"][5]["data"][i]

        # 新增治愈
        curedRelative = result["component"][0]["trend"]["list"][6]["data"][i]

        # 新增死亡
        diedRelative = result["component"][0]["trend"]["list"][7]["data"][i]

        # 新增境外
        overseasInputRelative = result["component"][0]["trend"]["list"][9]["data"][i]

        # 新增本土
        nativeRelative = result["component"][0]["trend"]["list"][9]["data"][i]

        history[ds] = {"curConfirm": curConfirm, "crued": crued, "died": died, "confirmedRelative": confirmedRelative,
                       "asymptomaticRelative": asymptomaticRelative, "curedRelative": curedRelative,
                       "diedRelative": diedRelative, "overseasInputRelative": overseasInputRelative,
                       "nativeRelative": nativeRelative}
    # 最新全国总数据
    # 时间
    t = int(result["component"][0]["summaryDataIn"]["relativeTime"])
    updatatime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))

    # 累计确诊
    confirmed = result["component"][0]["summaryDataIn"]["confirmed"]

    # 累计治愈
    cured = result["component"][0]["summaryDataIn"]["cured"]

    # 累计死亡
    died = result["component"][0]["summaryDataIn"]["died"]

    # 新增确诊
    confirmedRelative = result["component"][0]["summaryDataIn"]["confirmedRelative"]

    # 新增无症状
    asymptomaticRelative = result["component"][0]["summaryDataIn"]["asymptomaticRelative"]

    # 新增治愈
    curedRelative = result["component"][0]["summaryDataIn"]["curedRelative"]

    # 新增死亡
    diedRelative = result["component"][0]["summaryDataIn"]["diedRelative"]

    # 新增境外
    overseasInputRelative = result["component"][0]["summaryDataIn"]["overseasInputRelative"]

    # 新增本土
    nativeRelative = result["component"][0]["summaryDataIn"]["unOverseasInputNewAdd"]

    # 现有确诊
    curConfirm = result["component"][0]["summaryDataIn"]["curConfirm"]

    # 现有本土
    curLocalConfirm = result["component"][0]["summaryDataIn"]["curLocalConfirm"]

    # 现有境外
    curOverseasInput = result["component"][0]["summaryDataIn"]["curOverseasInput"]

    # 现有无症状
    asymptomatic = result["component"][0]["summaryDataIn"]["asymptomatic"]

    now = [updatatime, confirmed, cured, died, confirmedRelative, asymptomaticRelative, curedRelative,
           diedRelative, overseasInputRelative, nativeRelative, curConfirm,
           curLocalConfirm, curOverseasInput, asymptomatic]
    # 获取最新数据

    city_details = []
    province_details = []
    highDangerousAreas = []
    middleDangerousAreas = []
    relativeTime = result["component"][0]["mapLastUpdatedTime"]
    tup = time.strptime(relativeTime, "%Y.%m.%d %H:%M")  # 匹配时间
    update_time = time.strftime("%Y-%m-%d %H:%M", tup)  # 改变时间格式
    for pro_infos in result["component"][0]["caseList"]:
        # 省（直辖市/自治区）
        province = pro_infos["area"]
        # 新增确诊
        confirmedRelative = pro_infos["confirmedRelative"]
        # 新增死亡
        diedRelative = pro_infos["diedRelative"]
        # 新增治愈
        curedRelative = pro_infos["curedRelative"]
        # 新增疑似
        asymptomaticRelative = pro_infos["asymptomaticRelative"]
        # 新增本土
        nativeRelative = pro_infos["nativeRelative"]
        # 新增境外
        overseasInputRelative = pro_infos["overseasInputRelative"]
        # 累计确诊
        confirmed = pro_infos["confirmed"]
        # 现有确诊
        curConfirm = pro_infos["curConfirm"]
        province_details.append(
            [update_time, province, confirmedRelative, diedRelative, curedRelative, asymptomaticRelative,
             nativeRelative, overseasInputRelative, confirmed, curConfirm])
        for city_infos in pro_infos["subList"]:
            # 市（区）
            city = city_infos["city"]
            # 新增确诊
            confirmedRelative = city_infos["confirmedRelative"]
            # 新增疑似
            asymptomaticRelative = city_infos["asymptomaticRelative"]
            # 新增本土
            nativeRelative = city_infos["nativeRelative"]
            city_details.append([update_time, province, city, confirmedRelative, asymptomaticRelative, nativeRelative])
            templen = len(city_infos["dangerousAreas"]["subList"])
            if templen != 0:
                for list in city_infos["dangerousAreas"]["subList"]:
                    if list["level"] == '中风险':
                        area = list["area"]
                        middleDangerousAreas.append([province, city, area])
                    elif list["level"] == '高风险':
                        area = list["area"]
                        highDangerousAreas.append([province, city, area])

    # 数据检错 ，将数据中缺失的部分用 -1 进行标记
    for item in province_details:
        for items in range(0, 8):
            if item[items] == '':
                item[items] = -1

    for item in city_details:
        for items in range(0, 6):
            if item[items] == '':
                item[items] = -1
    return history, now, province_details, city_details, highDangerousAreas, middleDangerousAreas
